/*
	file:ProxyServer.cpp
	author:luoo
	date:2015 04 12
*/

#include <iostream>
#include <sys/socket.h>
#include <sys/types.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <iostream>
#include <libxml/parser.h>
#include <string.h>
#include <stdlib.h>  
#include <errno.h>  
#include <netdb.h>
#include <unistd.h>
using namespace std;
#define MAXDATASIZE 4098
#define PROXYPORT 8087
#define PROXYSERVERIP "127.0.0.1"

class ProxyThread
{
	public:
		char buf[MAXDATASIZE];
		char *registeredproxy="";
		int pnumbytes,pnew_fd,psockfd;
		struct sockaddr_in server_addr; 
		char * registermsg;
		ProxyThread();
		~ProxyThread();
		int InitConnection();
		char * SRD(char * buffer); //Send_Receive_Decode
};

ProxyThread::ProxyThread(){
	registermsg="<Envelope type=\"pregister\"></Envelope>";
}

ProxyThread::~ProxyThread(){
	close(pnew_fd);
}

int ProxyThread::InitConnection(){
    if ( ( psockfd = socket ( AF_INET , SOCK_STREAM , 0)) == - 1) { 
        perror("socket error"); 
        return -1; 
    } 
    memset ( &server_addr, 0, sizeof(struct sockaddr)); 
    server_addr. sin_family = AF_INET; 
    server_addr. sin_port = htons (PROXYPORT); 
    server_addr. sin_addr. s_addr = inet_addr(PROXYSERVERIP); 
    if ( connect ( psockfd, ( struct sockaddr * ) & server_addr, sizeof( struct sockaddr ) ) == -1) { 
        perror ("cannot connect to a proxyserver..."); 
        return -1;
    }
}

char * ProxyThread::SRD(char * buffer){ 
	//send
    if ( send ( psockfd, buffer , strlen(buffer), 0) == -1) { 
        perror ( "send error" ); 
        return NULL;
    }
    //receive
    if ( ( pnumbytes = recv ( psockfd, buf, MAXDATASIZE, 0) ) == -1) { 
        perror( "recv error" ); 
        return NULL; 
    } 
    if ( pnumbytes) { 
        buf[pnumbytes] = '\0'; 
        //printf("received: %s\n",buf); 
    }
    return buf;
}
